package com.c.ctgproject.adapter.doorkeeper

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.c.ctgproject.R
import com.c.ctgproject.entity.ListCarWaitingEntrance
import com.c.ctgproject.ui.doorkeeper.car.CarWaitActivity
import com.c.ctgproject.utils.handlerNull
import java.lang.StringBuilder

/**
 *作者:LiBW
 *创建日期: 2020/8/17
 *描述: 待入场车辆信息
 */
class DoorkeeperCarListAdapter(private val context: Context, private val carList: List<ListCarWaitingEntrance.DataBean.ListBean>) : RecyclerView.Adapter<DoorkeeperCarListAdapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.item_doorkeeper_list, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val data = carList[position]
        val carPlate = StringBuilder(data.ownerPlate.handlerNull())
        if (data.realPlate.handlerNull().isNotEmpty()) {
            carPlate.append("(${data.realPlate.handlerNull()})")
        }
        holder.mCarNumberTV.text = carPlate.handlerNull()

        holder.mOwnerNameTV.text = data.trailerDriverName.handlerNull()
        holder.mTrailerTimeTV.text = data.realCltTime.handlerNull()
        holder.mTrailerPlateTV.text = data.trailerDriverPlate.handlerNull()
        holder.mConfirmEnterBTN.setOnClickListener {
            if (data.carId != null) {
                (context as CarWaitActivity).takePhoto(data.carId.handlerNull())
            }
        }
    }

    override fun getItemCount(): Int {
        return carList.size
    }

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val mConfirmEnterBTN: Button = itemView.findViewById(R.id.mConfirmEnterBTN)
        val mCarNumberTV: TextView = itemView.findViewById(R.id.mCarNumberTV)
        val mOwnerNameTV: TextView = itemView.findViewById(R.id.mOwnerNameTV)
        val mTrailerPlateTV: TextView = itemView.findViewById(R.id.mTrailerPlateTV)
        val mTrailerTimeTV: TextView = itemView.findViewById(R.id.mTrailerTimeTV)
    }
}